#include "stm32f10x.h"                 // Device header
#include "Delay.h"
#include "Btn.h"
#include "OLED.h"
#include "CountSensor.h"
#include "Encoder.h"
#include "Timer.h"
#include "PWM.h"
#include "Servo.h"
#include "Motor.h"
#include "InputCapture.h"
#include "Encoder_TIM.h"
#include "AD.h"
#include "DMAConfig.h"
#include "Serial.h"
#include "I2C_Soft.h"
#include "MPU6050.h"
#include "W25Q64.h"
#include "SPI_Soft.h"
#include "SPI_Periph.h"
#include "MyRTC.h"
#include "ESP8266.h"
#include <string.h>

int open = 0;
char *response = "HTTP/1.1 200 OK\r\n"
"Date: Fri, 15 Mar 2024 01:52:56 +0000\r\n"
"Connection: close\r\n"
"Content-Type: text/html; charset=UTF-8\r\n"
"Content-Length: 93\r\n\r\n"
"<html>"
"<h1 style=\"font-size:100px;\" >Welcome to 410! <br />The door is opening</h1>"
"</html>\r\n\r\n";
int main(void)
{
	//Timer_Int2_Init();
	Serial_Init2();
	Servo_Init();
	OLED_Init();
	ESP8266_Init();
	printf("hello");
	ESP8266_StartServer();
	OLED_ShowString(3,1,"Open Count: 00");
	OLED_ShowString(4,1,"UART Buff: 0000");
	while(1){
		OLED_ShowNum(4,12,Serial_GetDataPointer(),4);
		Servo_SetAngle(0);
		if(ESP8266_WaitFor("openthedoor",10000)) {
			open++;
			OLED_ShowNum(3,13,open,2);
			Servo_SetAngle(0);
			Delay_ms(200);
			Serial_ClearRxString();
			Serial_SendString("AT+CIPSEND=0,230\r\n");
			ESP8266_WaitFor(">",1000000);
			printf("sending %d...\r\n",strlen((char*)response));
			Serial_SendString(response);
			Servo_SetAngle(180);
			Delay_s(2);
			Servo_SetAngle(0);
			Delay_s(1);
		} else {
			Serial_ClearRxString();
		}
	}
}

void TIM2_IRQHandler(void)
{
	if(TIM_GetITStatus(TIM2,TIM_IT_Update) != SET)
		return;
	TIM_ClearITPendingBit(TIM2,TIM_IT_Update);
}
